<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="no-cache">
<title>Insert title here</title>
<%
	//防止浏览器产生缓存
	response.setHeader("Pragma","No-Cache"); 
	response.setHeader("Cache-Control","No-Cache"); 
	response.setDateHeader("Expires", 0); 

	//定义基础路径，从DispatcherServlet获取数据
	String path = request.getContextPath() + "/";	
	String resourcesPath = path + "resources/";
	String omPath = resourcesPath + "operamasks-ui-2.0/";		
	
%>
<link href="<%=omPath%>css/default/om-default.css" rel="stylesheet"	type="text/css" />
<script src="<%=omPath%>js/jquery.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/jquery.xml2json.js" type="text/javascript"></script>
<script src="<%=omPath%>js/operamasks-ui.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/om-dialog-url.js" type="text/javascript"></script>
<style type="text/css">
	.search-button{
	padding-left: 50px;
	}
   	.myinput input{
    border:1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput input:focus{border: 1px solid #3A76C2;}
   #searchTable td{
   padding-top: 10px;
   }
   #searchTable .mytable-left{
   padding-left: 10px;
   text-align: right;
   width : 100px;
   }
   #searchTable .mytable-right{
   padding-right: 10px;
   width : 150px;
   }
</style>
<script type="text/javascript">
var path = "${pageContext.request.contextPath}" + "/";
var searchButton = null;
var measureDocGrid = null;
var qualityGrid = null;
var analysisGrid = null;
var searchFiled = null;
var dialogAnaInput = null;
var mainTool = null;

var searchUrl = path + "rm/filterQuery.html";
var addUrl = path +"rm/gridAdd.html";
var deleteUrl = path+"rm/gridDelete.html";
var anaDecideUrl = path + "rm/QualityCtl/decideAna.html";
var dialogAnaInputUrl = path+"rm/QualityCtl/anaInput.html?view=sm/newRm/matQualityMgr-Do";

$(function() {
	initVars();
	loadPageLayout();
	loadSearchPanel();
	loadTool();
	loadGrid();
	initDialogAnaInput();
	search();
});

function initVars(){
	searchButton = $("#search-button"); // 生成搜索按钮
	measureDocGrid = $("#measureDocGrid");
	qualityGrid = $("#qualityGrid");
	analysisGrid = $("#analysisGrid");
	searchFiled = $("#search-field");// 生成搜索字段
	dialogAnaInput = $("#dialog-AnaInput"); // 生成成分录入对话框
	mainTool = $("#maintool"); // 生成工具栏
}

function loadPageLayout() {
	var panels = [ {
		id : "north-panel",
		title : "搜索",
		region : "north",
		resizable : false,
		collapsible : false,
		height : 150
	}, {
		id : "center-panel",
		title : "质检批列表：",
		region : "center",
	}, {
		id : "east-panel",
		title : "质检批成分",
		region : "east",
		resizable : false,
		collapsible : true,
		width : 300,
	}, {
		id : "south-panel",
		title : "与质检批相关的计量单列表：",
		region : "south",
		resizable : false,
		collapsible : false,
		height : 155,
	} ];
	var config = {
		panels : panels,
		spacing : 3,
	};
	$("#page").omBorderLayout(config);
}

function loadSearchPanel(){
	$('#search-field input').each(function() {
		$(this).keyup(function(event) {
			if (event.keyCode == 13) {
				search();
			}
		});
	});
	$('#queryDeliveryType').omCombo({
		dataSource : [ {
			text : '直供',
			value : '直供'
		}, {
			text : '配送',
			value : '配送'
		} , {
			text : '调拨',
			value : '调拨'
		} ],
		width : '162px'
	});
	$('#queryAnaDecide').omCombo({
		dataSource : [ {
			text : '未确定',
			value : 'N'
		}, {
			text : '已确定',
			value : 'Y'
		} ],
		inputField:function(data,index){
		    return data.text+'('+data.value+')';
		},
		value : 'N',
		width : '162px'
	});
	$("#queryReceiveTimeS").omCalendar({showTime : true});
	$("#queryReceiveTimeE").omCalendar({showTime : true});
	$("#queryReceiveTimeS").css("width","140px");
	$("#queryReceiveTimeE").css("width","140px");
	searchButton.omButton({
		icons:{left : path + 'resources/sm/images/search.png'},
		width:70,
		onClick:function(){
			search();
		}
	});
}

function loadTool(){
	var buttons = [];
	var anaInput = {
			id : "anaInput",
			label : "成分输入",
			onClick : function() {
				var selections = qualityGrid.omGrid('getSelections', true);
				if (selections.length != 1) {
					$.omMessageBox.alert({
						content : '请选择一行记录',
					});
					return false;
				}
				showDialogAnaInput('成分输入', selections[0]);
			}
	};
	var anaDecide = {
			id : "anaDecide",
			label : "成分确定",
			onClick : function() {
				var selections = qualityGrid.omGrid('getSelections', true);
				if (selections.length != 1) {
					$.omMessageBox.alert({
						content : '请选择一行记录',
					});
					return false;
				}	
				$.omMessageBox.confirm(
				{
					title: "提示",
					content: "确定要确定选中记录成分?",
					onClose: function( value )
					{
						if( value ) decideAna( selections[0] );
					}
				});
			}
		};
	buttons.push({
		separtor : true
	});
	buttons.push(anaInput);
	buttons.push({
		separtor : true
	});
	buttons.push(anaDecide);
	buttons.push({
		separtor : true
	});
	// 加载工具栏
	mainTool.omButtonbar({
		btns : buttons
	});
}
function decideAna(rowData){
	var info = {
			"qualityBatch" : rowData['qualityBatch'],
	};
	$.post(anaDecideUrl,info,
			function(data, textStatus)
			{
		     if( data.success )
				{
		    	 	$.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
		    	 	qualityGrid.omGrid( "reload" );
				}
		     else
		    	 {
					$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
		    	 }						
			}
			, "json" 
			);
}
function loadGrid(){
	qualityGrid.omGrid({
		limit:30,
		singleSelect:true,
		height:475,
		colModel:[
			{header : '质检批号', name : 'qualityBatch', align : 'center', width : '130', sort:'serverSide'},
			{header : '计量单号', name : 'measureId', align : 'center', width : '130', sort:'serverSide'},
			{header : '物料编码', name : 'materialId', align : 'center', width : '100', sort:'serverSide'},
			{header : '物料描述', name : 'materialDesc', align : 'center', width : '130', sort:'serverSide'},
			{header : '收货时间', name : 'receiveTime', align : 'center', width : '135', sort:'serverSide'},
			{header : '毛重(KG)', name : 'grossWeight', align : 'center', width : '60', sort:'serverSide'},
			{header : '皮重(KG)', name : 'tareWeight', align : 'center', width : '60', sort:'serverSide'},
			{header : '净重(KG)', name : 'netWeight', align : 'center', width : '60', sort:'serverSide'},
		         ],
         onRowClick : function(rowIndex, rowData, event) {
        	 var group ={
        				op : "and",
        				rules : [{op : 'equal', field : 'measureId', value : rowData['measureId'], type : 'string'}]
        		};
        	 var group2 ={
       				op : "and",
       				rules :[{op : 'equal', field : 'qualityBatch', value : rowData['qualityBatch'], type : 'string'}]
       			};
        	 var extraData = {
        				"domain" : "SmRmMeasureDoc",
        				"rules" : JSON.stringify(group),
        		};
        	
         	 var extraData2 = {
      				"domain" : "SmRmQualityElement",
      				"rules" : JSON.stringify(group2),
      			};
        	 measureDocGrid.omGrid({
        			dataSource : searchUrl,
        			extraData : extraData, 
        			method:'POST'
        		}, 'reload');
        	
        	 analysisGrid.omGrid({
     			dataSource : searchUrl,
     			extraData : extraData2, 
     			method:'POST'
     		}, 'reload');
         }
	});
	measureDocGrid.omGrid({
		limit:0,
		singleSelect:true,
		height:128,
		colModel:[
			{header : '计量单号', name : 'measureId', align : 'center', width : '130', sort:'serverSide'},
			{header : '状态', name : 'status', align : 'center', width : '80', sort:'serverSide'},
			{header : '物料编码', name : 'materialId', align : 'center', width : '80', sort:'serverSide'},
			{header : '物料描述', name : 'materialDesc', align : 'center', width : '160', sort:'serverSide'},
			{header : '进厂类型', name : 'deliveryType', align : 'center', width : '100', sort:'serverSide'},
			{header : '净重', name : 'netWeight', align : 'center', width : '100', sort:'serverSide'},
			{header : '主车号', name : 'cartNumber', align : 'center', width : '80', sort:'serverSide'},
		         ]
	});
	analysisGrid.omGrid({
		limit:0,
		singleSelect:true,
		height:503,
		colModel:[
			{header : '元素名称', name : 'qualItem', align : 'center', width : '60', sort:'serverSide'},
			{header : '元素值', name : 'qualItemVal', align : 'center', width : '50', sort:'serverSide'},
		         ]
	});
}

function search(){
	var rules =[], i=0;
	$(':input','#searchTable').each(function() {
		if($.trim(this.name)!="")
			if( $(this).val() != null &&  $.trim($(this).val()) != "" 
					&& typeof($(this).attr('op')) != "undefined" && typeof($(this).attr('ty')) != "undefined")
				rules[i++] = {op : $(this).attr('op'), field : $(this).attr('name'), value : $(this).val(), type : $(this).attr('ty')};
	});
	rules[i++] = {op : 'equal', field : 'status', value : 'RECEIVE', type : 'string'};
	var group ={
			op : "and",
			rules : rules
	};
	var extraData = {
			"domain" : "SmRmMeasureDoc",
			"rules" : i==0?null:JSON.stringify(group),
	};
	qualityGrid.omGrid({
		dataSource : searchUrl,
		extraData : extraData, 
		method:'POST'
	}, 'reload');
	var state = $('#queryAnaDecide', searchFiled).omCombo('value');
	if (state == "N") {
		mainTool.show();
	} else if (state == "Y") {
		mainTool.hide();
	}
}
function initDialogAnaInput() {

	var frameId = "AnaInput";
	var buttons = [ {
		text : "关闭",
		click : function() {
			dialogAnaInput.omDialog("close");// 关闭dialog
		}
	} ];
	dialogAnaInput.omDialog({
		width : 600,
		height : 470,
		autoOpen : false,
		modal : true,
		resizable : false,
		targetmode : "iframe",
		name : frameId,
		url : dialogAnaInputUrl,
		buttons : buttons,
		onClose : function(event) {
			analysisGrid.omGrid('reload');
			}
	});

	// 显示dialog并初始化里面的输入框的数据
	showDialogAnaInput = function(title, rowData) {
		$('#AnaInput').contents().find("#qualityBatch").val(rowData["qualityBatch"]);
		$('#AnaInput').contents().find("#materialId").val(rowData["materialId"]);
		var childWindow = document.getElementById(frameId).contentWindow;
		childWindow.search();
		dialogAnaInput.omDialog("option", "title", title);
		dialogAnaInput.omDialog("open");// 显示dialog
	};

};

</script>
</head>
<body>
	<div id="page" style="height:835px;">
		<div id="north-panel" >
			<div id="search-field">
			<table id="searchTable">
				<tr>
					<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">进厂类型:</font></span></td>
					<td class="mytable-right"><span class="myinput"><input id="queryDeliveryType" name="deliveryType" op="equal" ty="string" type="text" /></span></td>
					<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">计量单号:</font></span></td>
					<td class="mytable-right"><span class="myinput"><input id="queryMeasureId" name="measureId" op="startwith" ty="string" type="text" /></span></td>
					<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">质检批号:</font></span></td>
					<td class="mytable-right"><span class="myinput"><input id="queryQualityBatch" name="qualityBatch" op="startwith" ty="string" type="text" /></span></td>
					<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">成分确定:</font></span></td>
					<td class="mytable-right"><span class="myinput"><input id="queryAnaDecide" name="anaDecide" op="equal" ty="string" type="text" /></span></td>
				</tr>
				<tr>
					<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料编码:</font></span></td>
					<td class="mytable-right"><span class="myinput"><input id="queryMaterialId" name="materialId" op="startwith" ty="string" type="text" /></span></td>
					<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料描述:</font></span></td>
					<td class="mytable-right"><span class="myinput"><input id="queryMaterialDesc" name="materialDesc" op="like" ty="string" type="text" /></span></td>
					<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">收货时间从:</font></span></td>
					<td class="mytable-right"><span class="myinput"><input id="queryReceiveTimeS" name="receiveTime" op="greaterorequal" ty="date" type="text" /></span></td>
					<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">收货时间到:</font></span></td>
					<td class="mytable-right"><span class="myinput"><input id="queryReceiveTimeE" name="receiveTime" op="lessorequal" ty="date" type="text" /></span></td>
				</tr>
			</table>
			</div>
			<br />
			<div class="search-button"><span id="search-button">搜索</span></div>
		</div>
		<div id="center-panel" >			
			<div id='maintool'></div>
			<table id="qualityGrid"></table>
		</div>		
		<div id="east-panel" >			
			<table id="analysisGrid"></table>			
		</div>
		<div id="south-panel" >
			<table id="measureDocGrid"></table>
		</div>
	</div>
	<div id="dialog-AnaInput"></div>
	<div id="dialog-AnaInput-DirectRecheck"></div>	
</body>
</html>